#include "SceneManager.h"
#include "SceneRendererBridge.h"
#include "Renderer.h"
#include "SceneDrawable.h"
#include "Camera.h"


SceneManager::SceneManager(int width,int height)
{
	wWidth = width;
	wHeight = height;

	rendererBrigde = new SceneRendererBridge();
	renderer = new Renderer(rendererBrigde);
}


SceneManager::~SceneManager(void)
{
	RemoveAllCameras();

	if(rendererBrigde != nullptr)
		delete rendererBrigde;
	if(renderer != nullptr)
		delete renderer;
}

void SceneManager::DrawScene()
{
	for(drawablesIt = drawables.begin();drawablesIt != drawables.end();++drawablesIt)
	{
		SceneDrawable * drawable = (*drawablesIt);
		if(drawable->IsVisible())
		{
			if(drawable->InFrustrum())
			{
				drawable->Draw(renderer);
			}
		}
	}
}


//CAMERA 
Camera * SceneManager::CreateCamera(std::string camName)
{
	Camera * cam = new Camera(camName);
	cam->SetAspectRatio((float)(wWidth/wHeight));
	cameras.push_back(cam);
	return cam;
}

void SceneManager::SetActiveCamera(Camera * camera)
{
	activeCamera = camera;
	rendererBrigde->SetActiveCamera(activeCamera);
}

Camera * SceneManager::GetActiveCamera()
{
	return activeCamera;
}

void SceneManager::RemoveCamera(Camera * camera)
{
	if(!cameras.empty() && camera != nullptr)
		for(camerasIt = cameras.begin();camerasIt != cameras.end();++camerasIt)
		{
			if((*camerasIt) == camera)
			{
				delete camera;
				camerasIt = cameras.erase(camerasIt);
				break;
			}
		}
}

void SceneManager::RemoveAllCameras()
{
	for(camerasIt = cameras.begin();camerasIt != cameras.end();++camerasIt)
	{
		if((*camerasIt) != nullptr)
		delete (*camerasIt);
	}
	cameras.clear();
}

//------------